package com.honey.compilation.sql.ddl;

import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.dml.AbstractSelect;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.utils.StringUtility;

public class CreateView extends  Create implements SqlScript {
	
	/** 数据库结构 */
	private String schema;
	
	/** 视图名称 */
	private String view;
	
	/** 查询条件 */
	private AbstractSelect select;
	
	/**
	 * 构造函数
	 * @param view 视图名称
	 */
	public CreateView(String view){
		this(null, view, null);
	}
	
	/**
	 * 构造函数
	 * @param view 视图名称
	 * @param select 查询条件
	 */
	public CreateView(String view, AbstractSelect select ){
		this(null, view, select);
	}

	/**
	 * 构造函数
	 * @param schema 数据库结构
	 * @param view 视图名称
	 * @param select 查询条件
	 */
	public CreateView(String schema, String view, AbstractSelect select ){
		this.schema = schema;
		this.view = view;
		this.select = select;
	}

	
	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public String getView() {
		return view;
	}

	public AbstractSelect getSelect() {
		return select;
	}

	public void setSelect(AbstractSelect select) {
		this.select = select;
	}

	public CharacterBuilder compiledContent(int indentLevel) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();
		if(StringUtility.stringHasValue(view)&& select != null){
			select.getComments().clear();
			answer.append(super.compiledContent(indentLevel))
			.append(SqlKeyWord.VIEW);
			if(StringUtility.stringHasValue(schema)){
				answer.append(schema).append(SqlKeyWord.OPERATION_PERIOD);
			}
			answer
			.append(view).
			append(SqlKeyWord.OPERATION_SPACE)
			.append(SqlKeyWord.AS);
			IndentSpace.newLine(answer);
			answer.append(select.compiledContent(indentLevel) )
			;
		}
		
		return answer;
	}
}
